package mao.thinkinjava.interupt;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

/**
 * @author Totoro
 * @date 2020/5/11 20:18
 */
public class TestInterrupt {
    private static  ExecutorService exec = Executors.newCachedThreadPool();
    public static void test(Runnable r) throws Exception{
        Future<?> submit = exec.submit(r);
        TimeUnit.MILLISECONDS.sleep(100);
        exec.shutdownNow();
        submit.cancel(true);
    }

    public static void main(String[] args) throws Exception {
//        ServerSocket serverSocket = new ServerSocket(8080);
//        Socket localhost = new Socket("localhost", 8080);
//        InputStream in = localhost.getInputStream();
//        test(new IOBlocking(in));
//        in.close();
//        localhost.close();
//        serverSocket.close();
        //System.exit(0);

        test(new IOBlocking(System.in));
        System.out.println(System.in);
        System.out.println("closing "+System.in.getClass().getName());
        //为何System.in无法关闭。
        System.in.close();
        System.out.println("after closing "+System.in);
        System.exit(0);
    }
}
